package com.greateffect.littlebud.lib.utils;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.media.ExifInterface;
import android.net.Uri;
import android.os.Build;
import android.os.Environment;
import android.provider.MediaStore;
import android.support.v4.app.Fragment;
import android.support.v4.content.FileProvider;
import android.support.v7.app.AppCompatActivity;

import com.greateffect.littlebud.lib.utilcode.ToastUtils;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Date;
import java.util.UUID;

/**
 * @author zengcs@vip.qq.com
 * @version v1.0
 * @date 2018/07/16 14:06
 */
public final class XtImageUtils {
    private XtImageUtils() {
    }

    /**
     * 调用系统相机并保存到系统相册
     */
    public static File startCameraInDICM(Fragment fragment, int requestCode) {
        Context context = fragment.getContext();
        if (context == null) return null;

        Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);//打开相机的Intent
        if (intent.resolveActivity(context.getPackageManager()) != null) {//这句作用是如果没有相机则该应用不会闪退，要是不加这句则当系统没有相机应用的时候该应用会闪退
            String fileName = "IMG_".concat(JDateKit.dateToStr("yyyyMMdd_HHmmss", new Date()));
            String imagePath = Environment.getExternalStorageDirectory().getAbsolutePath()
                    .concat(File.separator)
                    .concat(Environment.DIRECTORY_DCIM)
                    .concat(File.separator)
                    .concat("Camera")
                    .concat(File.separator)
                    .concat(fileName)
                    .concat(".jpg");
            File imageFile = new File(imagePath);
            if (!imageFile.getParentFile().exists()) {
                imageFile.getParentFile().mkdirs();
            }
            Uri imgUri;
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
                imgUri = FileProvider.getUriForFile(context, JUriUtil.TAG_FILE_PROVIDER, imageFile);///7.0以上要通过FileProvider将File转化为Uri
            } else {
                imgUri = Uri.fromFile(imageFile);//7.0以下则直接使用Uri的fromFile方法将File转化为Uri
            }
            intent.putExtra(MediaStore.EXTRA_OUTPUT, imgUri);//将用于输出的文件Uri传递给相机
            fragment.startActivityForResult(intent, requestCode);
            return imageFile;
        }
        return null;
    }

    /**
     * 调用相机
     *
     * @param requestCode
     * @return
     */
    public static File startCamera(AppCompatActivity activity, int requestCode) {
        Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);//打开相机的Intent
        if (intent.resolveActivity(activity.getPackageManager()) != null) {//这句作用是如果没有相机则该应用不会闪退，要是不加这句则当系统没有相机应用的时候该应用会闪退
            File imageFile;//创建用来保存照片的文件
            try {
                imageFile = File.createTempFile(UUID.randomUUID().toString(), ".jpg", activity.getExternalCacheDir());
                Uri imgUri;
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
                    imgUri = FileProvider.getUriForFile(activity, JUriUtil.TAG_FILE_PROVIDER, imageFile);///7.0以上要通过FileProvider将File转化为Uri
                } else {
                    imgUri = Uri.fromFile(imageFile);//7.0以下则直接使用Uri的fromFile方法将File转化为Uri
                }
                intent.putExtra(MediaStore.EXTRA_OUTPUT, imgUri);//将用于输出的文件Uri传递给相机
                activity.startActivityForResult(intent, requestCode);
                return imageFile;
            } catch (IOException e) {
                e.printStackTrace();
                ToastUtils.showLong("创建图片文件失败！");
            }
        } else {
            ToastUtils.showLong("调用系统相机失败！");
        }
        return null;
    }

    /**
     * 调用相机
     *
     * @param requestCode
     * @return
     */
    public static File startCamera(Fragment fragment, int requestCode) {
        Context context = fragment.getContext();
        if (context == null) return null;

        Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);//打开相机的Intent
        if (intent.resolveActivity(context.getPackageManager()) != null) {//这句作用是如果没有相机则该应用不会闪退，要是不加这句则当系统没有相机应用的时候该应用会闪退
            File imageFile;//创建用来保存照片的文件
            try {
                imageFile = File.createTempFile(UUID.randomUUID().toString(), ".jpg", context.getExternalCacheDir());
                Uri imgUri;
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
                    imgUri = FileProvider.getUriForFile(context, JUriUtil.TAG_FILE_PROVIDER, imageFile);///7.0以上要通过FileProvider将File转化为Uri
                } else {
                    imgUri = Uri.fromFile(imageFile);//7.0以下则直接使用Uri的fromFile方法将File转化为Uri
                }
                intent.putExtra(MediaStore.EXTRA_OUTPUT, imgUri);//将用于输出的文件Uri传递给相机
                fragment.startActivityForResult(intent, requestCode);
                return imageFile;
            } catch (IOException e) {
                e.printStackTrace();
                ToastUtils.showLong("创建图片文件失败！");
            }
        } else {
            ToastUtils.showLong("调用系统相机失败！");
        }
        return null;
    }

    /**
     * 对图片进行压缩
     *
     * @param path  原图片文件
     * @param nFile 新图片文件
     * @return
     */
    public static File compressImage(File path, File nFile, int maxKb) {
        if (path == null || !path.exists())
            return null;
        BitmapFactory.Options newOpts = new BitmapFactory.Options();
        // 开始读入图片，此时把options.inJustDecodeBounds 设回true了
        newOpts.inJustDecodeBounds = true;
        BitmapFactory.decodeFile(path.toString(), newOpts);
        Bitmap bitmap;// 此时返回bm为空

        newOpts.inJustDecodeBounds = false;
        int w = newOpts.outWidth;
        int h = newOpts.outHeight;
        // 现在主流手机比较多是800*480分辨率，所以高和宽我们设置为
        float hh = 1920f;// 这里设置高度为800f
        float ww = 1080f;// 这里设置宽度为480f
        // 缩放比。由于是固定比例缩放，只用高或者宽其中一个数据进行计算即可
        int be = 1;// be=1表示不缩放
        if (w > h && w > ww) {// 如果宽度大的话根据宽度固定大小缩放
            be = (int) (newOpts.outWidth / ww);
        } else if (w < h && h > hh) {// 如果高度高的话根据宽度固定大小缩放
            be = (int) (newOpts.outHeight / hh);
        }
        if (be <= 0)
            be = 1;
        newOpts.inSampleSize = be;// 设置缩放比例

        bitmap = BitmapFactory.decodeFile(path.toString(), newOpts);

        //修正图片旋转
        int degress = readPictureDegree(path.toString());
        if (degress != 0) {
            Matrix matrix = new Matrix();
            matrix.postRotate(degress);
            bitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
        }

        File newFile = compressImage(nFile, bitmap, maxKb);

        return newFile == null ? path : newFile;
    }

    public static File compressImage(File newFile, Bitmap image, int imgMaxKb) {
        if (image == null || newFile == null)
            return null;

        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        image.compress(Bitmap.CompressFormat.JPEG, 100, baos);// 质量压缩方法，这里100表示不压缩，把压缩后的数据存放到baos中
        int quality = 100;
        //options非线性
        while (baos.toByteArray().length / 1024 > imgMaxKb) { // 循环判断如果压缩后图片是否大于100kb,大于继续压缩
            baos.reset();// 重置baos即清空baos
            quality -= 10;// 每次都减少10
            JLogUtil.d("compressImage", "quality = " + quality);
            if (quality == 10) {
                image.compress(Bitmap.CompressFormat.JPEG, quality, baos);// 这里压缩options%，把压缩后的数据存放到baos中
                break;
            } else {
                image.compress(Bitmap.CompressFormat.JPEG, quality, baos);// 这里压缩options%，把压缩后的数据存放到baos中
            }
        }
        JLogUtil.d("compressImage", "图片压缩结束");
        FileOutputStream out = null;
        try {
            out = new FileOutputStream(newFile);
            out.write(baos.toByteArray());
        } catch (FileNotFoundException e) {
            e.printStackTrace();
            JLogUtil.d("compressImage", e.getMessage());
            return null;
        } catch (IOException e) {
            e.printStackTrace();
            JLogUtil.d("compressImage", e.getMessage());
            return null;
        } finally {
            try {
                if (out != null) {
                    out.flush();
                    out.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
            if (image != null)
                image.recycle();
        }

        return newFile;
    }

    /**
     * 获取图片旋转角度
     *
     * @param path
     * @return
     */
    public static int readPictureDegree(String path) {
        int degree = 0;
        try {
            ExifInterface exifInterface = new ExifInterface(path);
            int orientation = exifInterface.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);
            switch (orientation) {
                case ExifInterface.ORIENTATION_ROTATE_90:
                    degree = 90;
                    break;
                case ExifInterface.ORIENTATION_ROTATE_180:
                    degree = 180;
                    break;
                case ExifInterface.ORIENTATION_ROTATE_270:
                    degree = 270;
                    break;
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return degree;
    }

    /**
     * 显示拍照时的角度
     *
     * @param angle
     * @param bitmap
     * @return
     */
    public static Bitmap rotaingImageView(int angle, Bitmap bitmap) {
        //旋转图片 动作
        Matrix matrix = new Matrix();
        matrix.postRotate(angle);
        // 创建新的图片
        Bitmap resizedBitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
        return resizedBitmap;
    }
}
